#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        paramrForm.py
# Author:    KuKei
# Create Date: 2011-10-23
# Description: 文章管理部分表单
# Modify Date: 2011-10-23

from django import forms
from sd.cms.forms import BaseForm, BaseModelForm
from sd.cms.models.paramModel import *
from sd.cms.lib.utility import *


class GlobalForm(BaseModelForm):
    theme = forms.ChoiceField(label="主题", required=True)
    
    def __init__(self, baseview=None, modelData = None):
        BaseModelForm.__init__(self, baseview, modelData)
        self.fields['theme'].choices = Global.objects.toOptions(empty=('','请选择主题'))
        
    class Meta:
        model = Global
        fields = ('title','subtitle','keywork','describe','sumlength','cachelevel', 'theme')

class GlobalParamForm(BaseModelForm):
    def clean_code(self):
        code = self.data['code'] if self.data.has_key('code') else ''
        if not isEng(code.encode('utf-8')):
            raise forms.ValidationError("地址名只能是字母、数字或下划线！")
        if self.bv.request.POST:
            if GlobalParam.objects.filter(code=code).exclude(id=self.id).count() > 0:
                raise forms.ValidationError("编码重复，请更改！")
        return code
    
    class Meta:
        model = GlobalParam
        fields = ('kind','name','code','value','describe','orderby','autoload','issys','style')
        widgets = {
            'autoload': forms.CheckboxInput,
            'kind': forms.HiddenInput,
            'issys': forms.HiddenInput,
            'value': forms.Textarea(attrs={'class':'textarea1'}),
        }

class ParamForm(BaseModelForm):
    class Meta:
        model = Param
        fields = ('orderby','kind','code','name','value','describe','isdisplay')
        widgets = {
            'isdisplay': forms.CheckboxInput,
            'kind': forms.HiddenInput
        }
        
class ColumnForm(BaseModelForm):
    model_id = forms.ChoiceField(label="模型", required=True)
    
    def __init__(self, baseview=None, modelData = None):
        BaseModelForm.__init__(self, baseview, modelData)
        self.fields['model_id'].choices = Param.objects.toOptions(kind=1)
        
    class Meta:
        model = Column
        fields = ('orderby','name','value','model_id','describe','isdisplay')
        widgets = {
            'isdisplay': forms.CheckboxInput,
        }
        
class CatalogForm(BaseModelForm):
    column_id  = forms.ChoiceField(label="栏目", required=True)
    parent  = forms.ChoiceField(label="上级目录", required=True)
    
    def __init__(self, baseview=None, modelData = None):
        BaseModelForm.__init__(self, baseview, modelData)
        #放在初始化函数中填充choices是为了实时更新;同时为了提高效率，较少和数据库的交互将需要的数据在memcache中缓存
        self.fields['column_id'].choices = Column.objects.toOptions(empty=('','请选择'))
        self.fields['parent'].choices = Catalog.objects.toOptions(empty=('-1','无'))
        
    class Meta:
        model = Catalog
        fields = ('name','value','kind','level','parent','column_id','orderby','islast','isdisplay')
        widgets = {
            'isdisplay': forms.CheckboxInput,
            'islast': forms.CheckboxInput,
            #'parent': forms.Select
        }
        
    def clean_orderby(self):
        orderby = self.data['orderby']
        if orderby:
            pass
        else:
            raise forms.ValidationError("显示顺序不能为空!")
        return orderby
    
    
    
